package com.mp.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mp.demo.entity.vo.SysUserVo;
import com.mp.demo.utils.DozerUtils;
import org.dozer.Mapper;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import java.util.*;
import com.mp.demo.common.Result;
import  com.mp.demo.service.SysUserService;
import  com.mp.demo.entity.SysUser;

/**
* <p>
    *  前端控制器
    * </p>
*
* @author barry
* @since 2020-08-18
*/
@RestController
@Slf4j
@Api(value = "SysUser-Controller")
@RequestMapping("//sys-user")
 public class SysUserController {

    @Autowired
    private Mapper dozerMapper;
    @Autowired
    private SysUserService sysUserService;

    @ApiOperation(value = "获取sysUser-list-all")
    @GetMapping("/list-all")
    @ResponseBody
    public Result listAll(@RequestParam(required = false) String search) throws Exception {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        //todo 需自行设置查询字段
        Collection<SysUser> sysUserList= sysUserService.list(wrapper);
        List<SysUserVo> sysUserVos = DozerUtils.mapList(dozerMapper, sysUserList, SysUserVo.class);
        return Result.success(sysUserVos);
    }

    @ApiOperation(value = "获取sysUser-list-page")
    @GetMapping("/list-page")
    @ResponseBody
    public Result listPage(@RequestParam(required = false,defaultValue = "0") Integer page
                            ,@RequestParam(required = false,defaultValue = "20") Integer limit
                            ,@RequestParam(required = false) String search) throws Exception {
        IPage<SysUser> iPage = new Page<>(page, limit);
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        //todo 需自行设置查询字段
        IPage<SysUser> resultPage =  sysUserService.page(iPage, wrapper);
        return Result.success(resultPage);
    }

    @ApiOperation(value = "查询sysUser-id")
    @GetMapping("/query/{id}")
    @ResponseBody
    public Result query(@PathVariable Integer id) throws Exception {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        SysUser sysUser = sysUserService.getOne(wrapper);
        if (null != sysUser)
            return  Result.success(dozerMapper.map(sysUser,SysUserVo.class));
        return Result.success(sysUser);
    }

    @ApiOperation(value = "修改sysUser")
    @PostMapping("/update")
    @ResponseBody
    public Result update(@RequestBody  SysUser sysUser) throws Exception {
        Boolean flag = sysUserService.updateById(sysUser);
        return Result.defaultMsg(flag);
    }

    @ApiOperation(value = "删除sysUser")
    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Result delete(@PathVariable Integer id) throws Exception {
        Boolean flag = sysUserService.removeById(id);
        return Result.defaultMsg(flag);
    }

    @ApiOperation(value = "添加sysUser")
    @PutMapping("/add")
    @ResponseBody
    public Result add(@RequestBody  SysUser sysUser) throws Exception {
        Boolean flag = sysUserService.save(sysUser);
        return Result.defaultMsg(flag);
    }
}
